package com.econ.powercloud.ems.service.dao.clickhouse;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.econ.powercloud.clickhouse.entity.EmsStatisticalYear;
import com.econ.powercloud.clickhouse.mapper.EmsStatisticalYearMapper;
import com.econ.powercloud.ems.pojo.requestVO.ReportRequestVO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Objects;

/**
 * @author jwc
 * @date 2022/10/31
 */
@Repository
@DS("clickhouse")
public class EmsStatisticalYearDAO extends ServiceImpl<EmsStatisticalYearMapper, EmsStatisticalYear> {

    public List<EmsStatisticalYear> selectByCondition(ReportRequestVO requestVO) {
        requestVO.setEquipmentId(Objects.isNull(requestVO.getEquipmentId()) ? StringUtils.EMPTY :requestVO.getEquipmentId());
        LambdaQueryWrapper<EmsStatisticalYear> wrapper = Wrappers.<EmsStatisticalYear>lambdaQuery()
                .le(EmsStatisticalYear::getTime, requestVO.getEndTime())
                .ge(EmsStatisticalYear::getTime, requestVO.getStartTime())
                .eq(StringUtils.isNotEmpty(requestVO.getStationId()), EmsStatisticalYear::getEnergyStationId, requestVO.getStationId())
                .eq(EmsStatisticalYear::getEnergyEquipmentId, requestVO.getEquipmentId())
                .eq(StringUtils.isNotEmpty(requestVO.getReportId()), EmsStatisticalYear::getReportId,requestVO.getReportId())
                .eq(StringUtils.isNotEmpty(requestVO.getDeviceCode()), EmsStatisticalYear::getDeviceCode,requestVO.getDeviceCode())
                .in(CollectionUtils.isNotEmpty(requestVO.getColumnIdList()), EmsStatisticalYear::getColumnId,requestVO.getColumnIdList());
        return this.list(wrapper);
    }

    public void insertBatch(List<EmsStatisticalYear> list){
        this.getBaseMapper().insertBatch(list);
    }

    public void deleteByReportIdAndTime(String reportId, Long time) {
        this.getBaseMapper().deleteByReportIdAndTime(reportId, time);
    }
}
